<template>
  <div>
    <!-- 面包写导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        class="tree-table"
        :data="cateList"
        :columns="columns"
        :show-row-hover="false"
        :border="true"
        :selection-type="false"
        :expand-type="false"
        show-index
        :index-text="'#'"
      >
        <template slot="isok" scope="scope">
          <i class="el-icon-success" style="color:lightgreen" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <template slot="order" scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <template slot="opt" scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页列表 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="cateInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="cateInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addDialogClosed">
        <el-form ref="abc" :rules="rules" :model="addcateForm" label-width="80px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addcateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              v-model="selectKeys"
              :options="parentList"
              :props="cascaderProps"
              @change="parentCatChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCateCom">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品分类传递的参数
      cateInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //   商品结果保存
      cateList: [],
      // 总数
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateDialogVisible: false,
      addcateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      rules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      parentList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      selectKeys: []
    }
  },
  methods: {
    //   获取商品分类数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', { params: this.cateInfo })
      if (res.meta.status !== 200) return this.$messgae.error('获取商品分类失败')
      this.$message.success('获取商品分类列表成功')
      this.cateList = res.data.result
      this.total = res.data.total
    },
    //
    handleSizeChange(size) {
      this.cateInfo.pagesize = size
      this.getCateList()
    },
    handleCurrentChange(num) {
      this.cateInfo.pagenum = num
      this.getCateList()
    },
    // 添加分类
    addCate() {
      this.addCateDialogVisible = true
      this.getParentList()
    },
    async getParentList() {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$messgae.error('获取父级分类列表失败')
      this.parentList = res.data
    },
    parentCatChange() {
      if (this.selectKeys.length > 0) {
        console.log('aaaaaaaa' + this.selectKeys.length)

        this.addcateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.addcateForm.cat_level = this.selectKeys.length
      } else {
        this.addcateForm.cat_pid = 0
        this.addcateForm.cat_level = 0
      }
    },
    addCateCom() {
      this.$refs.abc.validate(async validate => {
        if (!validate) return
        const { data: res } = await this.$http.post('categories', this.addcateForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    addDialogClosed() {
      this.addcateForm.cat_name = ''
      this.selectKeys = []
      this.addcateForm.cat_pid = 0
      this.addcateForm.cat_level = 0
    }
  },
  created() {
    //   调用商品分类方法
    this.getCateList()
  }
}
</script>

<style lang="less" scoped>
.tree-table {
  margin-top: 10px;
}
.el-cascader {
  width: 100%;
}
</style>
